package com.googlecode.monitoring.ws.model;

import no.cicero.testframework.config.Config;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Author: Sergiy Doroshenko
 * Date: Jun 15, 2010
 * Time: 1:41:42 PM
 */
public class ConfigBuilder {
	public static Config
	build(TestFrameworkConfig tfConfig) throws MalformedURLException {

		Config config = new Config();
		config.setTargetUrl(new URL(tfConfig.getTargetUrl()));
		config.setNamespace(tfConfig.getNamespace());
		config.setNamespacePrefix(tfConfig.getNamespacePrefix());
		config.setUseCaseLocation(new File(tfConfig.getUseCaseLocation()));
		Collection<Pattern> collection = new ArrayList<Pattern>();

		for (String s : tfConfig.getUseCaseFilters()) {
			collection.add(Pattern.compile(s));
		}
		config.setUseCaseFilters(collection);
		config.setTestCaseLocation(new File(tfConfig.getTestCaseLocation()));

		config.setUseHeader(tfConfig.isUseHeader());
		if (config.isUseHeader()) {
			config.setPassword(tfConfig.getCfpSecurity().getPassword());
			config.setUser(tfConfig.getCfpSecurity().getLogin());

			Map<String, String> params = new HashMap<String, String>();
			params.put("kontekst.modellId", tfConfig.getCfpContext().getModelId());
			params.put("kontekst.kanalId", tfConfig.getCfpContext().getChannelId());
			config.setParams(new HashMap<String, String>());
		}

		return config;
	}
}
